package com.bkgy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName ThreadConfig
 * @date 2022/12/28 15:11
 */
@Configuration
@EnableAsync // 启用异步任务
public class ThreadConfig {
    @Bean("executor")
    public ThreadPoolTaskExecutor executor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        executor.setCorePoolSize(10);
        //设置最大线程数
        executor.setMaxPoolSize(100);
        //线程使用的缓冲队列
        executor.setQueueCapacity(10);
        //设置程序关闭时要等待线程全部执行完
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //设置等待时间，超过等待时间后立即停止
        executor.setAwaitTerminationSeconds(60);
        //线程名称前缀
        executor.setThreadNamePrefix("Test-Async-");
        //初始化线程
        executor.initialize();
        return executor;
    }
}
